﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DelegateLambda
{
    class GenericDelegate
    {
        public void Test()
        {
            //定义泛型委托变量
            MyGenericDelegate<int> myDelegate1 = Add;
            MyGenericDelegate<double> myDelegate2 = Sub;

            Console.WriteLine("自定义泛型委托1:" + myDelegate1(10, 20));
            Console.WriteLine("自定义泛型委托2:" + myDelegate2(10.5, 5.8));

            //使用泛型委托：结合匿名方法
            MyGenericDelegate<int> myDelegate3 = delegate (int a, int b) { return a + b; };
            MyGenericDelegate<double> myDelegate4 = delegate (double a, double b) { return a - b; };
            Console.WriteLine("自定义泛型委托3:" + myDelegate3(10, 20));
            Console.WriteLine("自定义泛型委托4:" + myDelegate4(10.5, 5.8));

            //使用泛型委托：结合Lamdba表达式
            MyGenericDelegate<int> myDelegate5 = (a, b) => a + b;
            MyGenericDelegate<double> myDelegate6 = (a, b) => a - b;
            Console.WriteLine("自定义泛型委托3:" + myDelegate5(10, 20));
            Console.WriteLine("自定义泛型委托4:" + myDelegate6(10.5, 5.8));

        }
        public void Test1()
        {
            Func<int,int,int> myDelegate1 = Add;
            Func<double,double,double> myDelegate2 = Sub;
        }

        //定义方法
        public int Add(int a, int b)
        {
            return a + b;
        }
        public double Sub(double a, double b)
        {
            return a - b;
        }
    }
    //定义泛型委托
    public delegate T MyGenericDelegate<T>(T param1,T param2);
}
